package logic

import (
	"MyBlogv2/Servers/Article/article"
	"MyBlogv2/Servers/Article/internal/svc"
	"MyBlogv2/blog-common/models"
	"MyBlogv2/blog-common/redisUtil"
	"context"
	"gorm.io/gorm"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateVisitCountLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateVisitCountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateVisitCountLogic {
	return &UpdateVisitCountLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateVisitCountLogic) UpdateVisitCount(in *Article.ArticleRequest) (*Article.UpdateVisitCountResponse, error) {
	id := in.Id
	redisUtil.AddVisitCount(int(id))
	models.Db.Model(new(models.Article)).Where("id = ?", id).Update("visit_count", gorm.Expr("visit_count+1"))

	return &Article.UpdateVisitCountResponse{}, nil
}
